package com.wyd.resourcetj.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.wyd.resourcetj.dao.IMemberDao;
import com.wyd.resourcetj.dto.MemberDTO;
import com.wyd.resourcetj.model.Member;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * MemberServiceImpl
 */
@Service
public class MemberServiceImpl implements IMemberService{

    @Resource
	private IMemberDao  memberDao;
	
	@Override
	public Page<Member> findAllDSQL(MemberDTO dto) {
    	//Sort sort = new Sort(Sort.Direction.ASC,"id");
    
	//	Pageable page = new PageRequest((dto.getPage()-1)*dto.getLimit(),dto.getLimit(),sort );
        
        Sort sort = Sort.by(Sort.Order.asc("id"));
Pageable page = PageRequest.of((dto.getPage()-1)*dto.getLimit(), dto.getLimit(),sort);
		return memberDao.findAll(getWhereClause(dto),page);
	}

   /*  private String name;
    private String card_no;//会员卡号
    private String phone;
    private Date regdate;//入会时间
    private Integer status;//状态
    private String swpartment;//系
    private String profession;//专业
    private String grade;//年级 */
	
private Specification<Member> getWhereClause(final MemberDTO dto){
		
		return new Specification<Member>() {
			
			@Override
			public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				
				Predicate predicate = criteriaBuilder.conjunction();
				List<Expression<Boolean>> exList = predicate.getExpressions();
				
				if(dto.getName()!=null && !"".equals(dto.getName())) {
					exList.add(criteriaBuilder.like(root.<String>get("name"), "%"+dto.getName()+"%"));
                }
                if(dto.getCard_no()!=null && !"".equals(dto.getCard_no())) {
					exList.add(criteriaBuilder.equal(root.<String>get("card_no"), dto.getCard_no()));
                }
                if(dto.getPhone()!=null && !"".equals(dto.getPhone())) {
					exList.add(criteriaBuilder.equal(root.<String>get("phone"), dto.getPhone()));
                }
                if(dto.getBegin_regdate()!=null) {
					exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getBegin_regdate()));
                }
                if(dto.getEnd_regdate()!=null) {
					exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("regdate"), dto.getEnd_regdate()));
                }

                

                if(dto.getStatus()!=null ) {
					exList.add(criteriaBuilder.equal(root.<Integer>get("status"), dto.getStatus()));
                }
                if(dto.getSwpartment()!=null && !"".equals(dto.getSwpartment())) {
					exList.add(criteriaBuilder.equal(root.<String>get("swpartment"), dto.getSwpartment()));
                }
                if(dto.getProfession()!=null && !"".equals(dto.getProfession())) {
					exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<String>get("profession"), dto.getProfession()));
                }
                if(dto.getGrade()!=null && !"".equals(dto.getGrade())) {
					exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<String>get("grade"), dto.getGrade()));
                }


				
				return predicate;
					
			}
			
			
			
		};
		
		
		
	}

    @Override
    public int addAll(List<Member> list) {
        List<Member> memberList = memberDao.saveAll(list);
        return memberList.size();
    }
    
}